<?php
// 生成随机字符串（可指定长度）
function randomkeys($length=1) {
    $key = '';
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ'; // 字符池
    for ($a = 0; $a < $length; $a++) {
        $key .= $pattern{mt_rand(0,62)};
    }
    return $key;
}
// url添加a标签
function add_url_href($content = ''){
    if (empty($content)) return $content;

    $pattern = '/(http:\/\/t\.cn\/[0-9a-zA-Z]+)/i';
    $replacement = '<a href="$1" target="_blank">$1</a>';
    return preg_replace($pattern, $replacement, $content);
}

// 时间格式转换
function conv_time($time = ''){
    if (empty($time)) return $time;

    $re = strtotime($time);
    if ($re !== false){
        return date('Y-m-d H:i:s', $re);
    }
    return $time;
}

// 获取html标签中的url地址
function get_html_url($str){
    if (!$str) return '';
    $str = stristr ($str, 'http');

    if (!$str) return '';
    $len = strpos($str, '"');

    if (!$len) return '';

    return substr($str, 0, $len);
}
